﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MadeInPortugal.Class;
using MadeInPortugal;
using System.Web.Mail;

namespace MadeInPortugalWebApplication.WebControl
{
    public partial class emailWC : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FillCountry();

            Product prod = Session["Product"] as Product;
            lblTo.Text = prod.Enterprise.EnterpriseName;
            tbxSubject.Text = prod.ProductName;

            if (Session["UserId"] != null)
            {
                Enterprise user = new Enterprise();
                user = user.Get(int.Parse(Session["UserId"].ToString()));
                tbxName.Text = user.Name;
                tbxEmail.Text = user.Email;
                tbxCompany.Text = user.EnterpriseName;
            }

        }

        private void FillCountry()
        {
            imagem1.Src = Session["imgCountryFlag"].ToString();
            lblVisitorsIP.Text = Session["visitorIP"].ToString();
            lblVisitorsIP0.Text = Session["visitorCountry"].ToString();
            Countries country = new Countries();
            Countries[] countriList = country.GetAll();
            foreach (Countries item in countriList)
            {
                ddlCountries.Items.Add(new ListItem(item.Country, item.CountryCode));
            }
            ddlCountries.Items.FindByValue(Session["visitorCountry"].ToString()).Selected = true;
        }



        protected void lblAddTag_Click(object sender, EventArgs e)
        {
            const string SERVER = "relay-hosting.secureserver.net";
            // create mail message object
            MailMessage mail = new MailMessage();
            mail.From = "admin@portuguese-made.com"; // put the from address here
            Product prod = Session["Product"] as Product;
            mail.To = prod.Enterprise.Email;             // put to address here
            mail.Cc = tbxEmail.Text;
            mail.Subject = tbxSubject.Text;        // put subject here	
            mail.BodyFormat = MailFormat.Html; // enumeration
            mail.Body = tbxContent.Text + "<br/> Phone-" + tbxIndicativoPais.Text + "-" + tbxIndicativo.Text + "-" + tbxPhone;           // put body of email here
            mail.Priority = MailPriority.Normal; // enumeration
            SmtpMail.SmtpServer = SERVER;
            // and then send the mail
            try
            {
                SmtpMail.Send(mail);
                SetFeedBack(string.Format("Message sent."), Messages.FeedBackType.success);
            }
            catch (Exception ee)
            {
                SetFeedBack(string.Format("Error sending message.<br/>-{0}", ee.ToString()), Messages.FeedBackType.error);
            }
            mail = null; // free up resources

        }

        private void SetFeedBack(string htmlMessage, Messages.FeedBackType fType)
        {
            feedbackPanel.InnerHtml = htmlMessage;
            feedbackPanel.Attributes.Add("class", fType.ToString());
        }
    }
}